import logging

import pytest

from resource.pom.launcher import Launcher
from resource.pom.settings import Settings
from utils.manager import Images


class Test:

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device, factory_reset):
        logging.info('步骤1、长按桌面空白处，点击更多设置，开启手势导航开关')
        device.long_click(360, 640)
        device.refresh_layout()
        more = device.get_element_by_text('更多设置')
        device.click_element(more)
        device.refresh_layout()
        switch = device.get_element_by_type('Toggle')
        if switch.get('checked') == 'false':
            device.click_element(switch)
        device.wait(2)
        enable_gesture_pic = device.save_snapshot_to_local('gesture.jpeg')
        Images.crop_picture(enable_gesture_pic, y1=1208, y2=1280)
        similarity = Images.compare_image_similarity(enable_gesture_pic, Launcher.gesture_enabled_pic)
        device.wait(1)
        device.swipe(from_x=1, from_y=640, to_x=200, to_y=640)
        logging.info('预期结果1、开启后底部导航栏消失，可上滑进入recent页面，左右往里滑动返回')
        device.refresh_layout()
        navigation_bar_id = device.get_navigationb_winid()
        condition = {
            'type': 'Flex',
            'hostWindowId': navigation_bar_id
        }
        navigation_btns = device.get_elements_by_condition(condition)
        assert len(navigation_btns) == 0, '导航栏按钮数量{}!=0'.format(len(navigation_btns))

        setting_app = Settings
        device.start_ability(setting_app.bundle_name, setting_app.ability_name)
        device.assert_current_window(setting_app.window_name)

        device.swipe(from_x=1, from_y=640, to_x=200, to_y=640)
        device.assert_current_window(Launcher.entry_view)

        assert similarity > 0.6, '功能正常，但底部导航栏未消失'
